home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / matvec / ex7.d < prev   
Encoding:
Text File  |  1991-04-22  |  2.6 KB  |  88 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program computes a matrix - vector product, dividing the matrix into
  3.  * blocks, with multiple elements per processor. The matrix is of size M x N,
  4.  * where M must be a multiple of P1, and N must be a multiple of P2. */
  5.  
  6. #include "dino.h"
  7.  
  8. #define P1 4
  9. #define P2 3
  10. #define M 16
  11. #define N 9
  12.  
  13. environment node[P1:id1][P2:id2] {
  14.  
  15.   map AllBlock = [all][block];
  16.   map BlockAll = [block][all];  /* ==> These are examples of user defined
  17.                                        mappings.  The extra dimentions had
  18.                                        to be used because of a deficiency in
  19.                                        DINO mappings. */
  20.  
  21.   composite matvec (in a, in x, out y)
  22.   double distributed a[M][N] map BlockBlock;    /* Input matrix */
  23.   double distributed x[1][N] map AllBlock;      /* Input vector */
  24.   double distributed y[M][1] map BlockAll;      /* Result vector */
  25.  
  26.   {
  27.     int i, j;                   /* Looping variable */
  28.  
  29.     /* Loop through the elements of y[] mapped to this procesor */
  30.     for (i = id1 * M/P1; i < (id1 + 1) * M/P1; i++) {
  31.  
  32.       /* Compute the contribution of this processor to the final result */
  33.       y[i][0] = 0;
  34.       for (j = id2 * N/P2; j < (id2 + 1) * N/P2; j++)
  35.         y[i][0] += a[i][j] * x[0][j];
  36.     }
  37.  
  38.     /* Now, we do a sum across all processors in my row to compute the final
  39.      * result */
  40.     y[<id1 * M/P1,(id1 + 1) * M/P1 - 1>][0] =
  41.         gsum(y[<id1 * M/P1,(id1 + 1) * M/P1 - 1>][0])# {node[id1][]};
  42.                                 /* ==> This is an example of a reduction using
  43.                                        an explicit environment set, which is
  44.                                        placed in brackets after the "#" sign. */
  45.  
  46.   }
  47. }
  48.  
  49. environment host {
  50.  
  51.   double a[M][N];
  52.   double x[1][N];
  53.   double y[M][1];
  54.  
  55.   void main ()
  56.  
  57.   {
  58.     int i, j;           /* Looping variables */
  59.  
  60.     /* Set up the initial data for a[][] and v[] */
  61.     for (j = 0; j < N; j++) {
  62.       x[0][j] = j;
  63.       for (i = 0; i < M; i++)
  64.         a[i][j] = i + j;
  65.     }
  66.  
  67.     /* Print out the initial data */
  68.     printf ("Initial data for a:\n");
  69.     for (i = 0; i < M; i++) {
  70.       for (j = 0; j < N; j++)
  71.         printf ("%6.2f", a[i][j]);
  72.       printf ("\n");
  73.     }
  74.  
  75.     printf ("\nInitial data for x:\n");
  76.     for (i = 0; i < N; i++)
  77.       printf ("%6.2f\n", x[0][i]);
  78.  
  79.     /* Perform the computation */
  80.     matvec (a[][], x[][], y[][])#;
  81.  
  82.     /* Printout the resulting vector y */
  83.     printf ("\nResult data for y:\n");
  84.     for (i = 0; i < M; i++)
  85.       printf ("%6.2f\n", y[i][0]);
  86.   }
  87. }
  88.